package com.liao.rent.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.liao.rent.entity.SysUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author: ljt
 * @Description: mybaits数据自动填充处理器
 * @DateTime: 2024/12/5 下午9:38
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

//    //插入时的填充策略
//    @Override
//    public void insertFill(MetaObject metaObject) {
//        log.info("start insert fill ....");
//        //三个参数：字段名，字段值，元对象参数
//        this.setFieldValByName("createTime",new Date(),metaObject);
//        this.setFieldValByName("updateTime",new Date(),metaObject);
//    }
//    //修改时的填充策略
//    @Override
//    public void updateFill(MetaObject metaObject) {
//        log.info("start update fill ....");
//        this.setFieldValByName("updateTime",new Date(),metaObject);
//    }

    @Override
    public void insertFill(MetaObject metaObject) {
//        log.info("开始插入填充...");
//        获取当前用户信息
        SysUserDetails userDetails=null;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

            userDetails= (SysUserDetails) authentication.getPrincipal();

        }catch (Exception e){

        }


        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateTime",LocalDateTime.class, LocalDateTime.now());


        if (userDetails == null){
            this.setFieldValByName("createUser",1,metaObject);
            this.setFieldValByName("updateUser",1,metaObject);
            return;
        }
//        this.strictInsertFill(metaObject, "createUser", Integer.class, userDetails.getUser().getId());
//        this.strictInsertFill(metaObject,"updateUser",Integer.class, userDetails.getUser().getId());
        this.setFieldValByName("createUser",userDetails.getUser().getId(),metaObject);
        this.setFieldValByName("updateUser",userDetails.getUser().getId(),metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        SysUserDetails userDetails=null;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

            userDetails= (SysUserDetails) authentication.getPrincipal();

        }catch (Exception e){

        }




        //        获取当前用户信息
//        SysUserDetails userDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        log.info("开始更新填充...");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());


        if (userDetails == null){
            this.setFieldValByName("updateUser",1,metaObject);
            return;
        }

//        this.strictInsertFill(metaObject,"updateUser",Integer.class, userDetails.getUser().getId());
        this.setFieldValByName("updateUser",userDetails.getUser().getId(),metaObject);

    }
}
